package com.gc.web.controller.business;

import com.gc.business.domain.CommunityManage;
import com.gc.business.service.ICommunityManageService;
import com.gc.business.service.IElevatorInfoService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.page.PageDomain;
import com.gc.common.page.TableDataInfo;
import com.gc.common.support.Convert;
import com.gc.common.utils.IdUtils;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import com.gc.framework.web.page.TableSupport;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 小区管理 控制层
 * 
 * @author hruiyan
 * @date 2020-11-12
 */
@Slf4j
@Controller
@RequestMapping("/business/communityManage")
public class CommunityManageController extends BaseController {
    private String prefix = "business/communityManage";
	
	private final ICommunityManageService communityManageService;

	private final IElevatorInfoService elevatorInfoService;

	@Autowired
	public CommunityManageController(ICommunityManageService communityManageService,
									 IElevatorInfoService elevatorInfoService) {
		this.communityManageService = communityManageService;
		this.elevatorInfoService = elevatorInfoService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:communityManage:view")
	@GetMapping
	public String communityManage(Long companyId,ModelMap mmap){
		mmap.put("companyId",companyId == null ? getAttrId() == null ? 0 : getAttrId() : companyId);
	    return prefix + "/communityManage";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(String companyId,ModelMap mmap){
		mmap.put("companyId",companyId);
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap){
		CommunityManage communityManage = communityManageService.getById(id);
		mmap.put("communityManage", communityManage);
		return prefix + "/edit";
	}
	
	/**
	 * 查询小区管理列表
	 */
	@RequiresPermissions("business:communityManage:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(CommunityManage communityManage){
		List<CommunityManage> list = null;
		if(StringUtils.isNull(getAttrId())){
			startPage();
			list = communityManageService.list(communityManage);
			return Result.success(getDataTable(list));
		}else{
			//假分页
			PageDomain pageDomain = TableSupport.buildPageRequest();
			Integer pageNum = pageDomain.getPageNum();
			Integer pageSize = pageDomain.getPageSize();
			list = communityManageService.selectCommunityByAttrId(getAttrId());
			PageInfo pageList = getPageList(pageNum, pageSize, list);
			TableDataInfo rspData = new TableDataInfo();
			rspData.setCode(0);
			rspData.setRows(pageList.getList());
			rspData.setTotal(pageList.getTotal());
			return Result.success(rspData);
		}
	}

	/**
	 * 新增保存小区管理
	 */
	@RequiresPermissions("business:communityManage:add")
	@Log(title = "小区管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(CommunityManage communityManage){
		communityManage.setCommunityId(IdUtils.getPrimaryKey());
		communityManage.setCreateTime(new Date());
		communityManage.setCreateBy(ShiroUtils.getLoginName());
		if (StringUtils.isNotEmpty(communityManage.getLatitude()) && StringUtils.isNotEmpty(communityManage.getLongitude())){
			String address = elevatorInfoService.getLatAndLng(communityManage.getLatitude(), communityManage.getLongitude());
			communityManage.setDetailAddress(address);
		}
		communityManageService.save(communityManage);
		return Result.success();
	}

	/**
	 * 修改保存小区管理
	 */
	@RequiresPermissions("business:communityManage:edit")
	@Log(title = "小区管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(CommunityManage communityManage){
		communityManageService.updateById(communityManage);
		return Result.success();
	}
	
	/**
	 * 删除小区管理
	 */
	@RequiresPermissions("business:communityManage:remove")
	@Log(title = "小区管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		communityManageService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出小区管理列表
	 */
	@RequiresPermissions("business:communityManage:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(CommunityManage communityManage){
		List<CommunityManage> list = communityManageService.list(communityManage);
		ExcelUtil<CommunityManage> util = new ExcelUtil<>(CommunityManage.class);
		util.exportExcel(list, "communityManage");
		return Result.success();
	}
}
